import {createRouter, createWebHistory} from 'vue-router';
import Layout from "@/layout/index.vue";
import {createRouterGuards} from '@/permission';

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    hidden: true,
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    redirect: 'index',
    hidden: true,
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'dashboard', affix: true }
      },
    ],
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/screen',
    name: '数据大屏',
    component: () => import('@/views/largeScreen/index.vue'),
    hidden: true
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'redirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/profile/index.vue'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

export async function setupRouter(app) {
  // 创建路由守卫
  createRouterGuards(router);
  app.use(router);
  // 路由准备就绪后挂载APP实例
  await router.isReady();
}

export default router;
